package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.business.service.impl.StatementServiceImpl;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算单明细 控制器
 */
@Controller
// 注意：访问controller的路径可以 / 开头 或 不是/ 开头, 注意要跟管理填写对应的url保持一致
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementitem/";

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    // 明细
    @RequiresPermissions("system:statementItem:view")
    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model){
        // 查看是否 已结算，已结算则跳转 显示明细页面，未结算跳转编辑明细页面
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        // 设置 结算人的 id
        statement.setPayee(userService.get(statement.getPayeeId()));
        if (statement.getStatus().equals(Statement.STATUS_PAID)){
            // 已经支付，跳转结算单明细页面
            return prefix + "showDetail";
        }else {
            // 消费中，跳转 结算单明细编辑页面
            return prefix + "editDetail";
        }

    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }

    //添加结算单明细
    @RequiresPermissions("system:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    //如果使用集合写 ArrayList<> list, 注意不能使用List<> list,反射创建对象时，接口不能直接new 对象
    //========== @RequestBody 注解 =============
    public AjaxResult saveItems(@RequestBody ArrayList<StatementItem> statementItems){
        statementItemService.saveItems(statementItems);
        return AjaxResult.success();
    }

    //添加结算单明细
    @RequiresPermissions("system:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }


}
